<template>
  <div class="product-list-wrap">
    <!-- 搜索页面头部，包括搜索栏和 Tab 栏 -->
    <div class="product-list-header">
      <!-- 搜索栏左居中右布局 -->
      <header class="product-header wrap">
        <!-- 返回按钮标签 -->
        <i class="nbicon nbfanhui" @click="goBack"></i>
        <!-- 搜索主体 -->
        <div class="header-search">
          <i class="nbicon nbSearch"></i>
          <!-- 搜索主体内嵌入输入框 -->
          <input type="text" class="search-title" v-model="keyword" />
        </div>
        <!-- 搜索按钮 -->
        <span class="search-btn" @click="getSearch">搜索</span>
      </header>
      <van-tabs type="card" color="#1baeae" @click="changeTab">
        <van-tab title="推荐" name=""></van-tab>
        <van-tab title="新品" name="new"></van-tab>
        <van-tab title="价格" name="price"></van-tab>
      </van-tabs>
    </div>

    <!-- 滚动区域，内容部分 -->
    <div class="content">
      <van-pull-refresh
        v-model="refreshing"
        @refresh="onRefresh"
        class="product-list-refresh"
      >
        <!-- 滚动列表 -->
        <van-list
          v-model:loading="loading"
          :finished="finished"
          :finished-text="productList.length ? '没有更多了' : '搜索想要的商品'"
          @load="onLoad"
          @offset="10"
        >
          <!-- productList 为接口返回的商品列表 -->
          <template v-if="productList.length">
            <div
              class="product-item van-hairline--bottom"
              v-for="(item, index) in productList"
              :key="index"
              @click="productDetail(item)"
            >
              <!-- 商品图片 -->
              <img :src="$filters.prefix(item.goodsCoverImg)" />
              <!-- 商品信息 -->
              <div class="product-info">
                <!-- 商品名称 -->
                <p class="name">{{ item.goodsName }}</p>
                <!-- 商品副标题 -->
                <p class="subtitle">{{ item.goodsIntro }}</p>
                <!-- 商品价格 -->
                <span class="price">￥ {{ item.sellingPrice }}</span>
              </div>
            </div>
          </template>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>
  
<script>
import { reactive, toRefs } from "vue";
import { useRoute, useRouter } from "vue-router";
import { searchGood } from "service/goods";

export default {
  name: "ProductList",
  setup() {
    const route = useRoute();
    const router = useRouter();
    const state = reactive({
      keyword: "",
      loading: false,
      finished: false,
      productList: [],
      page: 1,
      totalPage: 0,
      orderBy: "",
      refreshing: false, //下拉刷新状态控制
    });
    // 获取数据的方法
    const initData = async () => {
    
        const {categoryId} = route.query
      //必须输入关键词搜索，否则接口报异常
      if (!categoryId &&!state.keyword) {
        state.finished = true;
        state.loading = false;
        return;
      }
      const { data } = await searchGood({
        pageNumber: state.page, //分页参数
        keyword: state.keyword, //关键词参数
        orderBy: state.orderBy, //排序参数
        goodsCategoryId: categoryId //商品分类 id
      });

      // 赋值给商品列表
      state.productList = state.productList.concat(data.list);
      // 商品总页数
      state.totalPage = data.totalPage;
      // 关闭加载
      state.loading = false;
      // 如果当前页数大于或者等于总页数，则说明数据全部加载完毕，没有下一页
      if (state.page >= data.totalPage) state.finished = true;
    };

    const onLoad = () => {
      // 当当前页的页数小于商品列表总页数时，page加1，再执行initData方法
      if (!state.refreshing && state.page < state.totalPage) {
        state.page = state.page + 1;
      }
      // 当下拉刷新状态为 true 时，重置 productList 为空数组，清理之前残留的数据
      if (state.refreshing) {
        state.productList = [];
        state.refreshing = false;
      }
      initData();
    };

    //重新加载，初始化参数
    const onRefresh = () => {
      (state.refreshing = true), (state.finished = false);
      state.loading = true;
      state.page = 1;
      onLoad();
    };

    // Tab 栏切换方法
    const changeTab = ({ name }) => {
      state.orderBy = name;
      onRefresh();
    };

    // 返回方法
    const goBack = () => {
      router.go(-1);
    };

    // 跳转至详情页面
    const productDetail = (item) => {
      router.push({
        path: `/product-detail/${item.goodsId}`,
        query: { id: item.goodsId },
      });
    };

    // 单击“搜索”按钮，触发 onRefresh 事件
    const getSearch = () => {
      onRefresh();
    };
    return {
      ...toRefs(state),
      onLoad,
      changeTab,
      goBack,
      getSearch,
      productDetail,
      onRefresh
    };
  },
};
</script>
  
<style lang="less" scoped>
@import "../theme/custom.less";
.product-list-header {
  .product-header {
    .fj();
    width: 100%;
    // 搜索栏高度
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    .boxSizing();
    font-size: 15px;
    color: #656771;
    z-index: 10000;
    .header-search {
      display: flex;
      flex: 1 0 0;
      line-height: 20px;
      margin: 10px;
      padding: 5px 0;
      color: #232326;
      background: #f7f7f7;
      overflow: hidden;
      .borderRadius(20px);
      .nbSearch {
        padding: 0 5px 0 20px;
        font-size: 17px;
      }
      .search-title {
        width: 100%;
        font-size: 12px;
        color: #666;
        background: #f7f7f7;
      }
    }
    .search-btn {
      height: 28px;
      margin: 8px 0;
      line-height: 28px;
      padding: 0 5px;
      color: #fff;
      background: @primary;
      .borderRadius(5px);
      margin-top: 10px;
    }
  }
}

/*通过css的计算数学calc动态计算滚动区域的高度，
这里的页面可视区域高度为100vh,减去搜索栏和Tab栏的高度 80px，
最终的高度便是滚动区域的高度 */
.content {
  height: calc(~"(100vh - 70px)");
  // 超出隐藏
  overflow: hidden;
  // Y轴设置可滚动
  overflow-y: scroll;
}
.product-item {
  .fj();
  width: 100%;
  padding: 16px;
  border-bottom: 1px solid #dcdcdc;
  img {
    width: 140px;
    height: 120px;
    .boxSizing();
  }
  .product-info {
    width: 56%;
    height: 120px;
    padding: 5px;
    text-align: left;
    .boxSizing();
    .name {
      line-height: 20px;
      font-size: 15px;
      color: #333;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .subtitle {
      line-height: 30px;
      font-size: 13px;
      color: #999;
      overflow: hidden;
    }
    .price {
      color: @primary;
      font-size: 16px;
    }
  }
}
</style>